﻿using Max.Ph5_2009A.NewShopping.IRespository;
using Max.Ph5_2009A.NewShopping.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.Ph5_2009A.NewShopping.Service
{
    public class BaseService<T> : IBaseService<T> where T : class,new()
    {
        IBaseRespository<T> _respository;
        IUnitOfWork _unitOfWork;

        public BaseService(IBaseRespository<T> respository,IUnitOfWork unitOfWork)
        {
            _respository = respository;
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 删除数据（单删除或批量删除）
        /// </summary>
        /// <param name="ts"></param>
        /// <returns>受影响行数</returns>
        public int Delete(List<T> ts)
        {
            _respository.Delete(ts);
            return _unitOfWork.SaveChanges();
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public int Insert(T t)
        {
            _respository.Insert(t);
           return  _unitOfWork.SaveChanges();
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> Select()
        {
            IQueryable<T> result = _respository.Select();
            return result;
        }

        /// <summary>
        /// 分页查询数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total">符合条件的总记录条数</param>
        /// <returns></returns>
        public IQueryable<T> Select(int pageIndex, int pageSize,out int total)
        {
            IQueryable <T> result = _respository.Select();
            total = result.Count();
            result = result.Skip((pageIndex-1)*pageSize).Take(pageSize);

            return result;
        }

        /// <summary>
        /// 分页带条件查询数据
        /// </summary>
        /// <param name="where"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public IQueryable<T> Select(System.Linq.Expressions.Expression<Func<T, bool>> where, int pageIndex, int pageSize, out int total)
        {
            IQueryable<T> result = _respository.Select().Where(where);
            total = result.Count();
            result = result.Skip((pageIndex - 1) * pageSize).Take(pageSize);

            return result;
        }

        /// <summary>
        /// 分页带条件和排序查询数据
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="where"></param>
        /// <param name="sort"></param>
        /// <param name="IsAsc"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public IQueryable<T> Select<TKey>(System.Linq.Expressions.Expression<Func<T, bool>> where, 
            System.Linq.Expressions.Expression<Func<T, TKey>> sort, bool IsAsc, 
            int pageIndex, int pageSize, out int total)
        {
            IQueryable<T> result = _respository.Select().Where(where);

            if (IsAsc)
            {
                result = result.OrderBy(sort);
            }
            else
            {
                result = result.OrderByDescending(sort);
            }

            total = result.Count();
            result = result.Skip((pageIndex - 1) * pageSize).Take(pageSize);

            return result;
        }

        public int Update(T t)
        {
            _respository.Update(t);
            return _unitOfWork.SaveChanges();
        }
    }
}
